package com.vito.framework.system.menu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vito.framework.base.controller.BaseController;
import com.vito.framework.system.menu.model.MenuBean;
import com.vito.framework.system.menu.service.MenuService;
import com.vito.framework.system.menu.service.impl.MenuTree;
import com.vito.framework.system.org.model.OrgBean;
import com.vito.framework.system.org.service.OrgManageService;
import com.vito.support.ApiResponse;
import com.vito.support.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***************************************************************
 * Name:        MenuAction.java     
 * Create At:   2010-04-22
 * Module:      菜单管理
 * Description: [菜单管理-菜单管理增删改查功能]
 * Author       zhaowentao
 * ------------------------------------------------------------
 * 修改历史
 * 序号  日期  修改人   修改原因
 * 1
 * 2
***************************************************************/
@RestController
@RequestMapping("${adminPath}/menu")
public class MenuController extends BaseController {
    
	@Autowired
	private MenuService menuService;	
	@Autowired
	private OrgManageService orgManageService;
	
	/**
     * Author:shanzhitong
     * Create At:2017-07-29
     * Description: [系统管理-初始页面，ajax查询菜单]
     * @return String
     */
	@PostMapping(value = "getPmenuList", consumes = "application/json;charset=UTF-8")
	public ApiResponse getPmenuList(HttpServletRequest request, @RequestBody JSONObject params) {
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if (null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}
		if(StringUtils.isEmpty(params.getString("param_menuLevel"))){
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}

		paramMap.put("param_menuLevel", params.getString("param_menuLevel"));
		try {
			List<MenuBean> pmenu = menuService.getPmenuList(paramMap);
			return ApiResponse.ok(pmenu);
		} catch (Exception e) {
			return ApiResponse.of(e);
		}
	}

	/**
     * Author:shanzhitong
     * Create At:2017-07-29
     * Description: [系统管理-初始页面，删除菜单]
     * @return String
     */
	@PostMapping(value = "deleteMenu", consumes = "application/json;charset=UTF-8")
	public ApiResponse deleteMenu(HttpServletRequest request, @RequestBody JSONObject params) {
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if (null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}
		String menuId=params.getString("menuId");
		String[] ids=menuId.split(",");
		try {
			paramMap.put("ids",ids);
			menuService.deleteMenu(paramMap);
			return ApiResponse.ok(ResultEnum.OK);
		} catch (Exception e) {
			return ApiResponse.of(e);
		}
	}

	@PostMapping(value = "addSave", consumes = "application/json;charset=UTF-8")
	public ApiResponse addSave(HttpServletRequest request, @RequestBody JSONObject params, MenuBean menuBean) {
  		Map<String, Object> paramMap = super.getParamMap(request,params);
		if (null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}
		try {
			
		    menuBean= JSONObject.toJavaObject(params, MenuBean.class);
			menuService.addMenu(menuBean);
			return ApiResponse.ok(ResultEnum.OK);
		} catch (Exception e) {
			return ApiResponse.of(e);
		}

	}

	@PostMapping(value = "editSave", consumes = "application/json;charset=UTF-8")
	public ApiResponse editSave(HttpServletRequest request, @RequestBody JSONObject params, MenuBean menuBean) {
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if (null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}
		
		try {
			
			menuBean= JSONObject.toJavaObject(params, MenuBean.class);
			menuService.editMenu(menuBean);

			return ApiResponse.ok(ResultEnum.OK);
		} catch (Exception e) {
			return ApiResponse.of(e);
		}
	}

	@PostMapping(value = "edit", consumes = "application/json;charset=UTF-8")
	public ApiResponse edit(HttpServletRequest request, @RequestBody JSONObject params) {
		Map<String, Object> resMap = new HashMap<>();
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if (null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}
		
		try {
			paramMap.put("menuId", params.getString("menuId"));
			resMap.put("menuBean", menuService.getMenuById(params.getString("menuId")));
			resMap.put("SILIST", menuService.getOrgResultList(params.getString("menuId")));

			return ApiResponse.ok(resMap);
		} catch (Exception e) {
			return ApiResponse.of(e);
		}
	}

	/**
	 * Author:zhaowentao
     * Create At:2010-04-22
     * Description: [菜单管理-查询菜单]
     * @return String
     */
	@PostMapping(value = "getMenuList", consumes = "application/json;charset=UTF-8")
	public ApiResponse getMenuList(HttpServletRequest request, @RequestBody JSONObject params,Model model) {
		Map<String, Object> resMap = new HashMap<>();
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if (null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}

		paramMap.put("param_keyword", params.getString("param_keyword"));
		try {
		    List<MenuBean> list = menuService.getMenuList(paramMap);
		    list = MenuTree.buildDivMenuTreeData(list);
			resMap.put("list", list);
			return ApiResponse.ok(resMap);
		} catch (Exception e) {
			return ApiResponse.of(e);
		}
	}
	//菜单授权机构  查询列表
	@PostMapping(value = "getOrgList", consumes = "application/json;charset=UTF-8")
	public ApiResponse getOrgList(HttpServletRequest request, @RequestBody JSONObject params,Model model) {
		Map<String, Object> resMap = new HashMap<>();
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if (null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}
		String menuId=params.getString("menuId");
		try {
		   List<OrgBean> list1=orgManageService.getOrgManageList(paramMap);
		   //已选机构
		   List<OrgBean> list2=orgManageService.getOrgManageByMenuId(menuId);
		
			resMap.put("list1", list1);
			resMap.put("list2", list2);
			return ApiResponse.ok(resMap);
		} catch (Exception e) {
			return ApiResponse.of(e);
		}
	}
	@PostMapping(value = "orgMenuSave", consumes = "application/json;charset=UTF-8")
	public ApiResponse orgMenuSave(HttpServletRequest request, @RequestBody JSONObject params,Model model) {
		Map<String, Object> resMap = new HashMap<>();
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if (null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}
		String orgMenuId=params.getString("orgMenuId");
		try {
			List<String> idList = JSONArray.parseArray(params.getJSONArray("dataArray").toJSONString(), String.class);
			String[] orgIds = idList.toArray(new String[idList.size()]);
//			if(orgIds != null && orgIds.length>0) {
//				for(int i=0;i<orgIds.length;i++) {
//					JSONObject jsonObject = JSONObject.parseObject(orgIds[i]);
//				}
//			}
			menuService.orgMenuSave(orgMenuId,orgIds);
			return ApiResponse.ok(ResultEnum.OK);
		} catch (Exception e) {
			return ApiResponse.of(e);
		}
	}
}